package tests;


import common.AutoTestUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.time.Duration;

public class PageByUnLogin extends AutoTestUtils {
    private static final Logger logger = LoggerFactory.getLogger(Class.class);
    public static String listurl = "http://127.0.0.1:8080/blog_list.html";
    public static String editurl = "http://127.0.0.1:8080/blog_edit.html";
    public static String detailurl = "http://127.0.0.1:8080/blog_detail.html";
    public static String updateurl = "http://127.0.0.1:8080/blog_update.html?blogId=10";
    private int passedTests = 0;

    public PageByUnLogin() {
        super("http://127.0.0.1:8080/blog_edit.html");
    }

    /**
     * 博客列表页未登录测试
     *
     * @throws IOException
     */
    public void ListPageByUnLogin()  {
        try {
            //1.处理警告弹窗
            driver.get(listurl);
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(6));
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert1 = driver.switchTo().alert();
            Thread.sleep(1000);
            alert1.accept();
            //2.检测是否跳转回登录页面
            //弹窗处理后才能进行屏幕截屏
            getScreenShot(getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName());
            String title = driver.getTitle();
            assert title.equals("博客登录页");
            passedTests++;
            logger.info("未登录：博客列表页未登录测试通过");
        } catch (Exception e) {
            logger.error("未登录：博客列表页未登录测试通过，异常信息：" + e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 博客详情页未登录测试
     * @throws IOException
     */
    public void DetailPageByUnLogin() throws IOException {
        try {
            driver.get(detailurl);
            //1.处理警告弹窗
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(4));
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            alert.accept();
            //2.检测是否跳转回登录页面
            //弹窗处理后才能进行屏幕截屏
            getScreenShot(getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName());
            String title = driver.getTitle();
            assert title.equals("博客登录页");
            passedTests++;
            logger.info("未登录：博客详情页未登录测试通过");
        } catch (Exception e) {
            logger.error("未登录：博客详情页未登录测试通过，异常信息：" + e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 博客编辑页未登录测试
     * @throws IOException
     */
    public void EditPageByUnLogin() throws IOException{
        try {
            //1.处理警告弹窗
            WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(4));
            wait.until(ExpectedConditions.alertIsPresent());
            Alert alert = driver.switchTo().alert();
            alert.accept();
            //2.检测是否跳转回登录页面
            //弹窗处理后才能进行屏幕截屏
            getScreenShot(getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName());
            String title = driver.getTitle();
            assert title.equals("博客登录页");
            passedTests++;
            logger.info("未登录：博客编辑页未登录测试通过");
            //driver.quit();
        } catch (Exception e) {
            logger.error("未登录：博客编辑页未登录测试通过，异常信息：" + e.getMessage());
            throw new RuntimeException(e);
        }
    }

    /**
     * 博客更新页未登录测试
     * @throws IOException
     */
    public void updatePageByUnLogin() throws IOException{
        driver.get(updateurl);
        //1.处理警告弹窗
        WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(4));
        wait.until(ExpectedConditions.alertIsPresent());
        Alert alert = driver.switchTo().alert();
        alert.accept();
        //2.检测是否跳转回登录页面
        //弹窗处理后才能进行屏幕截屏
        getScreenShot(getClass().getName(), new Object() {}.getClass().getEnclosingMethod().getName());
        String title = driver.getTitle();
        assert title.equals("博客登录页");
        passedTests++;
        logger.info("未登录：博客更新页未登录测试通过");
    }

    public void runAllTests() {
        int totalTests = 4;
        try {
            EditPageByUnLogin();
            ListPageByUnLogin();
            DetailPageByUnLogin();
            updatePageByUnLogin();
            logger.info("注册页总测试用例：" + passedTests + "/" + totalTests);
        } catch (Exception e) {
            logger.error("整体测试运行失败，异常信息：" + e.getMessage());
        }
    }
}